In a program until a particular condition is being satisfied, repetition of any operation is called looping.
There are two types of loop in C programming.
- Top tested
- Bottom Tested
Top Tested :- The loop in which condition is checked before the execution of statement block.
Bottom Tested :- The loop in which a single or a block of statements are executed at least once, and then the condition is evaluated.
In ‘C’ there are three types of loop:
- While loop
- Do while loop
- For loop
( 1 ) While loop:
In while loop, if the condition is found to be true then it executes a single or a block of statements. In while loop, After the condition 1, it again checks the condition and if the condition is found to be true then it again executes the single statement or a block of statements. These statements are executed continuously until the condition is false.
When the condition is false the loop will break and control will execute the statement written after the while block.
Syntax : –
while ( condition )
{
statement 1;
——————
——————
——————-
}
( 2 ) Do-while loop:
This loop is used same as the while loop The main difference between while and do-while loop is that while loop first evaluates the condition and if the condition is true it executes a single or a block of statement.
In do-while loop, first it executes a single or block of statements then evaluates the condition. If the condition is true, it executes a single statement or a block of statements continuously until the condition becomes false.
Syntax :-
do {
statements;
—————-
—————-
} while ( condition )
{
statements;
————–
—————
}
( 3 ) For loop :-
For loop provides more facilities then while and do-while loop. The for loop has three expressions
syntax :-
part 1 part 2 part 3
for ( initialization; condition; increment / decrement )
{
statements;
——————-
——————
}
semicolon is necessary for separating these expression. The first expression is for counter, second expression is for condition and the third condition is for modification of counter.
Working of for loop :- First it initialize the counter, then checks the condition, if the condition is met then it executes a single statement or block of statements. After this expression 3. modifies the counter then it again checks the condition and if the condition is true then block of statements are executed continuously until the condition is true. When the condition became false the control executes the statement after the block statement of loop.
Nested loop :-
If in a block statement, we use one or more then one loops then it is called nested loop.
Loop Control Statements :-
Break Statement :-
Break statement is used generally with loops and switch statements. It is used to terminate a loop. This statement causes an immediate exit from that loop in which this statement appear. This statement does it work without checking condition-
Syntax :- break;
Continue :-
This statement is used only in loops. The continue statement is used for doing next iteration of loop. The loop does not terminate when this statement occur. But it skips the statements, which are after this statement in the block and transfer the control in the beginning of loop. In for loop it transfer the control on increment / decrement portion.
Syntax :- continue;
Go To Command :-
This is an unconditional control statement. where we use this statement the from of control is transffered to another part of the program without testing any other condition.
Syntax :- GO To Label;
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co